package com.swiftride.template;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONUtil;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.message.PushBatchDTO;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.swiftride.pojo.GtMessage;
import com.swiftride.properties.GtPushProperties;

import java.util.Map;

public class GtPushTemplate {


    private final GtPushProperties gtPushProperties;
    private final PushApi pushApi;
    private final UserApi userApi;


    public GtPushTemplate(GtPushProperties gtPushProperties) {
        this.gtPushProperties = gtPushProperties;
        ApiHelper apiHelper = creatApiHelper();
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        pushApi = apiHelper.creatApi(PushApi.class);
        userApi = apiHelper.creatApi(UserApi.class);
    }

    /**
     * Api参数配置,在此处修改
     *
     * @return 返回ApiHelper对象
     */
    private ApiHelper creatApiHelper() {
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        // 填写应用配置
        apiConfiguration.setAppId(gtPushProperties.getAppId());
        apiConfiguration.setAppKey(gtPushProperties.getAppKey());
        apiConfiguration.setMasterSecret(gtPushProperties.getMasterSecret());
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀
        apiConfiguration.setDomain(gtPushProperties.getGtUrl());
        // 实例化ApiHelper对象，用于创建接口对象
        return ApiHelper.build(apiConfiguration);
    }

    public void bindAlias(String cid, String alias) {
        CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
        cidAliasListDTO.add(new CidAliasListDTO.CidAlias(cid, alias));
        ApiResult<Void> apiResult = userApi.bindAlias(cidAliasListDTO);
        Assert.isTrue(0 == apiResult.getCode(), "别名绑定失败");
    }

    public void pushBatchByAlias(GtMessage gtMessage, String alias) {
        PushBatchDTO pushBatchDTO = new PushBatchDTO();
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        pushDTO.setRequestId(String.valueOf(System.currentTimeMillis()));
        //audience 推送目标用户设置
        Audience audience = new Audience();
        audience.addAlias(alias);
        pushDTO.setAudience(audience);
        //settings 推送条件设置
        Settings settings = new Settings();
        settings.setTtl(3600000);
        pushDTO.setSettings(settings);
        // 添加推送消息
        PushMessage pushMessage = new PushMessage();
        String gtMessageJsonStr = JSONUtil.toJsonStr(gtMessage);
        pushMessage.setTransmission(gtMessageJsonStr);
        pushDTO.setPushMessage(pushMessage);
        // 添加推送消息到推送对象
        pushBatchDTO.addPushDTO(pushDTO);
        ApiResult<Map<String, Map<String, String>>> mapApiResult = pushApi.pushBatchByAlias(pushBatchDTO);
        Assert.isTrue(0 == mapApiResult.getCode(), "根据别名发送失败");
    }
}
